package fileHandling.language;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import fileHandling.SettingsLoader;

import logic.nodes.nodeSettings.Settings;

import settings.CommonSettings;

/**
 * Provides static methods to load new language-files and to get a certain String of the currently loaded language.
 * 
 * @author Wasserleiche
 */
public class LanguageLoader {
		
	/** The basic language-path. */
	private static final String LANG_PATH = "data/lang/";
	
	/** Contains all Strings in mapped to the chosen language. */
	private static Settings languageData = new Settings();
	
	private static List<String> loadedFiles = new ArrayList<String>();
	
	private static final String NULL_STRING = "ERROR";
	
	private static final Random r = new Random();
	
	private static int hints;
	
	/**
	 * Returns all installed languages of the game.
	 * @return A {@link List} of all installed languages.
	 */
	public static String[] getInstalledLanguages() {
		List<String> languageList = new ArrayList<String>();
		File f = new File(LANG_PATH);
		File[] fileArray = f.listFiles();
		for(File file : fileArray) {
			if(file.isDirectory() && !file.getPath().contains(".")) {
				languageList.add(file.getName());
			}
		}
		String[] languages = new String[languageList.size()];
		return languageList.toArray(languages);
	}
	
	public static void loadAllLanguageFiles() {
		reset();
		
		File folder = new File(LANG_PATH + getLangString() + "/");
		
		File[] subFolders = folder.listFiles();
		for(File subFolder : subFolders) {
			if(!subFolder.getName().startsWith(".") && subFolder.isDirectory()) {
				File[] files = subFolder.listFiles();
				for(File file : files) {
					if(!file.isDirectory() && !file.getName().startsWith(".") && file.getName().endsWith(".xml")) {
						loadLanguageFile(subFolder.getName() + "/" + file.getName());
					}
				}
			}
		}
		
		
	}
	
	/**
	 * Loads the language-Strings containing in the given file into languageData. If this is the first file to 
	 * load, languageData will be initialized.
	 * @param file The file-name (and path) of the file to load.
	 */
	private static void loadLanguageFile(String file) {
		if(loadedFiles.contains(file)) return;
		
		Settings tempSettings = SettingsLoader.loadSettings(LANG_PATH + getLangString() + "/" + file);
		languageData.copySettings(tempSettings, false);
		loadedFiles.add(file);
		if(file.endsWith("hints.xml")) hints = tempSettings.getOptionMap().size();
	}
	
	private static String getLangString() {
		String language = CommonSettings.get().getLanguageString();
		if(language == null || language == "") language = "English";
		return language;
	}
	
	/**
	 * Returns the String of the current language of the given String.
	 * @param infoType The String to get the language-String from.
	 * @return The language-String of the given String. NULL_STRING, if no such mapping exists in languageData.
	 */
	public static String get(Lang infoType) {
		if(infoType == null) return "";
		return get(infoType.toString());
	}
	
	public static String get(String infoType) {
		String returnString = languageData.getValueOf(infoType);
		if(returnString == null) return NULL_STRING;
		return returnString.replace(";", "\n");
	}
	
	/**
	 * Reloads all language-files.
	 */
	public static void reset() {
		languageData = new Settings();
		loadedFiles.clear();
	}
	
	public static String getRandomHint() {
		int i = r.nextInt(hints);
		return get("hint" + i);
	}
}